import { createSlice } from '@reduxjs/toolkit'
const carSlice = createSlice({
  name: 'car',
  initialState: {
    carList: [],
  },
  reducers: {
    addCar(state, { payload }) {
      console.log('car:', payload)
      // 判断商品是否存在
      let index = state.carList.findIndex((item) => item.id === payload.id)
      if (index === -1) {
        state.carList = [...state.carList, { ...payload, buyNumber: 1 }]
      } else {
        state.carList[index].buyNumber++
      }
    },
  },
})
export const { addCar } = carSlice.actions
export default carSlice.reducer
